﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KinectPG.KinectGameLibrary.Enums;
using Microsoft.Kinect;
using System.Threading;

namespace KinectPG.KinectGameLibrary
{
    public class GestureRecognizer
    {
        #region Fields

        private Limb[] _limbs;
        private Thread recognizerThread;
        #endregion

        #region Properties

        public bool RightHandUp { get; set; }
        public bool LeftHandUp { get; set; }
        public bool BothHandsForward { get; set; }

        #endregion

        #region Constructors

        public GestureRecognizer()
        {
        }

        public GestureRecognizer(Limb[] limbs) : base()
        {
            recognizerThread = new Thread(new ThreadStart(RecognizeGestures));
            recognizerThread.IsBackground = true;
            recognizerThread.Start();
            this._limbs = limbs;
        }
        #endregion

        public void Stop()
        {
            this.recognizerThread.Abort();
        }

        public bool WaveGesture()
        {
            return false;
        }

        public void RightHandUpGesture()
        {
            if (_limbs[(int)JointType.HandRight].Y > _limbs[(int)JointType.ElbowRight].Y && (_limbs[(int)JointType.HandRight].X >= _limbs[(int)JointType.ElbowRight].X - 0.2 && _limbs[(int)JointType.HandRight].X <= _limbs[(int)JointType.ElbowRight].X + 0.2))
            {
                RightHandUp = true;
            }
            else
            {
                RightHandUp = false;
            }
        }

        public void LeftHandUpGesture()
        {
            if (_limbs[(int)JointType.HandLeft].Y > _limbs[(int)JointType.ElbowLeft].Y && (_limbs[(int)JointType.HandLeft].X >= _limbs[(int)JointType.ElbowLeft].X - 0.2 && _limbs[(int)JointType.HandLeft].X <= _limbs[(int)JointType.ElbowLeft].X + 0.2))
            {
                LeftHandUp = true;
            }
            else
            {
                LeftHandUp = false;
            }
        }

        public void BothHandsForwardGesture()
        {
            if (_limbs[(int)JointType.ShoulderCenter].Z - _limbs[(int)JointType.HandLeft].Z >= 0.5 && _limbs[(int)JointType.ShoulderCenter].Z - _limbs[(int)JointType.HandRight].Z >= 0.5)
            {
                BothHandsForward = true;
            }
            else
            {
                BothHandsForward = false;
            }
        }

        public void RecognizeGestures()
        {
            while (true)
            {
                RightHandUpGesture();
                LeftHandUpGesture();
                BothHandsForwardGesture();
                Thread.Sleep(500);
            }
        }

    }
}
